package com.servlet;

import com.model.Post;
import com.model.User;
import com.service.PostService;
import com.service.PostServiceImpl;
import com.util.Constant;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/addPost")
public class AddPostServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private PostService postService = new PostServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        
        // 获取表单参数
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String categoryIdStr = request.getParameter("categoryId");
        
        // 验证参数
        if (title == null || title.trim().isEmpty() || 
            content == null || content.trim().isEmpty() ||
            categoryIdStr == null || categoryIdStr.trim().isEmpty()) {
            request.setAttribute("errorMsg", "标题、内容和分类不能为空");
            request.getRequestDispatcher("/addPost.jsp").forward(request, response);
            return;
        }

        int categoryId;
        try {
            categoryId = Integer.parseInt(categoryIdStr);
        } catch (NumberFormatException e) {
            request.setAttribute("errorMsg", "分类参数错误");
            request.getRequestDispatcher("/addPost.jsp").forward(request, response);
            return;
        }

        // 获取当前登录用户
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(Constant.USER_SESSION);
        if (user == null) {
            response.sendRedirect(request.getContextPath() + "/login?error=请先登录");
            return;
        }

        // 封装帖子对象
        Post post = new Post();
        post.setTitle(title.trim());
        post.setContent(content.trim());
        post.setUserId(user.getId());
        post.setCategoryId(categoryId);

        // 调用服务层发布帖子
        try {
            boolean success = postService.addPost(post);
            if (success) {
                // 发布成功，重定向到帖子列表页
                response.sendRedirect(request.getContextPath() + "/PostList.jsp");
            } else {
                request.setAttribute("errorMsg", "发布帖子失败");
                request.getRequestDispatcher("/addPost.jsp").forward(request, response);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            request.setAttribute("errorMsg", "数据库错误，发布失败");
            request.getRequestDispatcher("/addPost.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // GET请求跳转到发布页面
        request.getRequestDispatcher("/addPost.jsp").forward(request, response);
    }
}